package com.atelier.plugin.projectWizard.util;

import java.io.*;

public class MavenArchetypeGenerator {
    public static boolean generateMavenArchetype(String outputDirectory, String groupId, String artifactId, String version, String packageValue, String mavenRepoUrl,String MavenPath) {
        try {
            // 读取bat文件内容
            InputStream inputStream = MavenArchetypeGenerator.class.getResourceAsStream("/createProject.bat");
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder batContent = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                batContent.append(line).append(System.lineSeparator());
            }
            reader.close();
            // 创建临时文件并写入bat内容
            File tempFile = File.createTempFile("createProject", ".bat");
            FileWriter writer = new FileWriter(tempFile);
            writer.write(batContent.toString());
            writer.close();
            System.out.println("绝对路径为:"+tempFile.getAbsolutePath());
            // 构建命令
            String[] command = {"cmd.exe", "/c", tempFile.getAbsolutePath(), groupId, artifactId, version, packageValue, mavenRepoUrl, outputDirectory,MavenPath};

            // 启动进程
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            processBuilder.redirectErrorStream(true);
            processBuilder.directory(new File(outputDirectory));
            Process process = processBuilder.start();

            // 读取命令输出
            InputStream processInputStream = process.getInputStream();
            BufferedReader processReader = new BufferedReader(new InputStreamReader(processInputStream));
            String processLine;
            while ((processLine = processReader.readLine()) != null) {
                System.out.println(processLine);
            }

            // 等待命令执行完成
            int exitCode = process.waitFor();
            // 删除临时文件
            tempFile.delete();
            if (exitCode == 0) {
                System.out.println("命令执行成功");
                return true;
            } else {
                System.out.println("命令执行失败");
            }

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean generateMavenArchetype(String tempPath) {
        try {
            // 读取bat文件内容
            InputStream inputStream = MavenArchetypeGenerator.class.getResourceAsStream("/temp.bat");
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder batContent = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                batContent.append(line).append(System.lineSeparator());
            }
            reader.close();
            // 创建临时文件并写入bat内容
            File tempFile = File.createTempFile("temp", ".bat");
            FileWriter writer = new FileWriter(tempFile);
            writer.write(batContent.toString());
            writer.close();
            System.out.println("绝对路径为:"+tempFile.getAbsolutePath());
            // 构建命令
            String[] command = {"cmd.exe", "/c", tempFile.getAbsolutePath(), tempPath};

            // 启动进程
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            processBuilder.redirectErrorStream(true);
            Process process = processBuilder.start();

            // 读取命令输出
            InputStream processInputStream = process.getInputStream();
            BufferedReader processReader = new BufferedReader(new InputStreamReader(processInputStream));
            String processLine;
            while ((processLine = processReader.readLine()) != null) {
                System.out.println("添加自定义骨架"+processLine);
            }

            // 等待命令执行完成
            int exitCode = process.waitFor();
            // 删除临时文件
            tempFile.delete();
            if (exitCode == 0) {
                System.out.println("命令执行成功");
                return true;
            } else {
                System.out.println("命令执行失败");
            }

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

}
